package com.yuan.util.collection;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 简化Map
 * @author YouYuan
 * @version 1.0
 * @CreateDate 2018/7/6 10:47
 * @UpdateDate 2018/7/6 10:47
 */
public class SimpleMap extends HashMap {

    public SimpleMap() {
    }

    public static SimpleMap by(Object key, Object value) {
        return (new SimpleMap()).set(key, value);
    }

    public static SimpleMap create() {
        return new SimpleMap();
    }

    public SimpleMap set(Object key, Object value) {
        super.put(key, value);
        return this;
    }

    public SimpleMap set(Map map) {
        super.putAll(map);
        return this;
    }

    public SimpleMap set(SimpleMap SimpleMap) {
        super.putAll(SimpleMap);
        return this;
    }

    public SimpleMap delete(Object key) {
        super.remove(key);
        return this;
    }

    public <T> T getAs(Object key) {
        return (T) this.get(key);
    }

    public String getStr(Object key) {
        Object s = this.get(key);
        return s != null?s.toString():null;
    }

    public Integer getInt(Object key) {
        Number n = (Number)this.get(key);
        return n != null?Integer.valueOf(n.intValue()):null;
    }

    public Long getLong(Object key) {
        Number n = (Number)this.get(key);
        return n != null?Long.valueOf(n.longValue()):null;
    }

    public Number getNumber(Object key) {
        return (Number)this.get(key);
    }

    public Boolean getBoolean(Object key) {
        return (Boolean)this.get(key);
    }

    public boolean notNull(Object key) {
        return this.get(key) != null;
    }

    public boolean isNull(Object key) {
        return this.get(key) == null;
    }

    public boolean isTrue(Object key) {
        Object value = this.get(key);
        return value instanceof Boolean && ((Boolean)value).booleanValue();
    }

    public boolean isFalse(Object key) {
        Object value = this.get(key);
        return value instanceof Boolean && !((Boolean)value).booleanValue();
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }

    public boolean equals(Object SimpleMap) {
        return SimpleMap instanceof SimpleMap && super.equals(SimpleMap);
    }
}
